/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

private ["_arry","_heloname","_callsign","_home","_airport","_egress","_ingress","_wepType1","_wepClass1","_wepType2","_wepClass2","_helo","_heloNewStation","_class","_mkrname","_asloc","_mkrname2","_asloc2","_stgtar","_helo_marker_distance","_dist1","_markerpos","_grp","_pilot","_gunner","_heloMoveWaypoint"];

/* create variables for all the settings sent to the script */
_arry = _this select 0;
_heloname = _arry select 0;
_callsign = _arry select 1;
_home = _arry select 2;
_airport = _arry select 3;
_egress = _arry select 4;
_ingress = _arry select 5;
_wepType1 = _arry select 6;
_wepClass1 = _arry select 7;
_wepType2 = _arry select 8;
_wepClass2 = _arry select 9;
_helo = _arry select 0;
_heloNewStation = true;
_class = typeof _helo;

/* create variables for all the markers shown on the map */
_mkrname = format ["%1station", _helo];
_asloc = createMarker [_mkrname, [0,0,0]];
_asloc setMarkerShape "ELLIPSE";
_mkrname setMarkerBrush "BORDER";
_mkrname setMarkerSize [100,100];
_mkrname setMarkerColor "ColorGreen";
_mkrname2 = format ["%1station2", _helo];
_asloc2 = createMarker [_mkrname2, [0,0,0]];
_asloc2 setMarkerShape "ICON";
_mkrname2 setMarkerSize [1,1];
_mkrname2 setMarkerType "mil_Dot";
_mkrname2 setMarkerColor "ColorGreen";
_mkrname2 setMarkerText _callsign;
// _stgtar = format ["'%1' setMarkerPos _pos,'%2' setMarkerPos _pos,RUM_ASclick = true",_mkrname,_mkrname2];

/* forces the server side helo object to become client side. */
[_helo] joinSilent player;
[_helo] joinSilent grpNull;

/* opens map, allows for user to select area for the stationed helo */
openMap [true,true];

_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = true; }, _mkrname] call BIS_fnc_addStackedEventHandler;

titleText ["Select a Station Point", "PLAIN DOWN"];
RUM_ASclick = false;
waitUntil {RUM_ASclick};

_mapclick = ["rum_mapclick_id", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
titleFadeOut 0;
sleep 1;
openMap [false,false];
RUM_ASclick = false;

_markerpos = getmarkerpos _mkrname;
_helo_marker_distance = _markerpos distance [getPos _helo select 0, getPos _helo select 1, 0];
_dist1 = round _helo_marker_distance;
	
/* checks to see if the helo is either on a LHD deck over water or on land - more debug at time */
if (surfaceIsWater (getpos _home)) then {
	player sidechat format["Heli is on LHD or Ship, %1",_helo];
	// _helo setPosASL _heliPosASL;
}else{
	player sidechat format["Heli is not over water, %1",_helo];
	// _helo setpos getpos _home;
};
	
/* checks the helo in as being in use, removed from PDA */
_helo setVariable ["RUM_inFlight", true, true];

/* create units to fly helo - client side */
sleep 1.5;
_grp = createGroup side player;
_pilot = _grp createUnit [RUM_AHEL_Pilot, getpos _home, [], 0, "FORM"];
_pilot moveinDriver _helo;
_gunner = _grp createUnit [RUM_AHEL_Pilot, getpos _home, [], 0, "FORM"];
_gunner moveinGunner _helo;

/* deletes any additional gunner created if any so there are no extra units running around */
if (!(_gunner in crew _helo)) then {
	deleteVehicle _gunner;
};

/* forces the server side helo object to become client side. */
[_helo] joinSilent _grp;
_helo setBehaviour "SAFE";
_helo setCombatMode "BLUE";

/* creates dialog from pilot to calling infantry */
["sendtostation",[player,_pilot,_callsign]] execVM "RUM\AHel\radiodialogs.sqf";
sleep 16;

/* start helo engines */
_helo engineOn true;

/* creates waypoints calling back to helo funtions for flight behavior */
for [{_n=1},{_n<=2},{_n=_n+1}] do {
	_mkrname = "";
	switch (_n) do {
		case 1: {
			[_mkrname,_grp,_helo,_egress] call RUM_AHel_moveWPTakeoff; 
			[_mkrname,_grp,_helo,_home,_egress] call RUM_AHel_moveWPNormal;
		};
		case 2: {
			_mkrname = format ["%1station",_helo];
			[_mkrname,_grp] call RUM_AHel_movewaypoint;[_mkrname,_grp,_heloNewStation,_helo] call RUM_AHel_moveWPStation;
		};
		default {
			hint "ooops!";	
		};
	};
};

/* ??? - maybe left from something else... not sure at this time 10/23/13 */
_heloMoveWaypoint = true;

/* waits until the helo has reached the station point - giving height for sloped locations to complete within */
waituntil{
	sleep 0.15;
	((getPos _helo select 2) <= (RUM_AHEL_flyStationHeight + 25));
};

/* completion radius to reach within */
sleep 5;
waitUntil{
	sleep 0.15;
	(!alive _helo) or (((((getPos _helo select 0) - (getmarkerpos _mkrname select 0))^2+((getPos _helo select 1) - (getmarkerpos _mkrname select 1))^2)^0.5) <= 100);
};

/* checks in when station area is reached and helo has settled */
["flightcheckin",[player,_pilot,_callsign]] execVM "RUM\Ahel\radiodialogs.sqf";

/* adds the helo and its settings to the stationed tab of the PDA - creates PV for multiplayer*/
RUM_AHelAircraft_onStation = RUM_AHelAircraft_onStation + [_arry];
publicVariable "RUM_AHelAircraft_onStation";

//hint format ["%1",RUM_FXDAircraft_onStation];

if(true)exitwith{};
